package vicarvision;

import doa.PersonDAO;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import models.Person;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author tom
 */
public class Vicarvision {


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
//        long t = System.currentTimeMillis();
//        long end = t + 5000;
//        vicarvision.Vicarvision.start_alanyzing();
//        while (System.currentTimeMillis() < end) {
////            System.out.println("run");
//            Person carlton = vicarvision.Vicarvision.getPerson();
//            PersonDAO.insertPerson(carlton);
////            System.out.println(vals + " = valance");
////            System.out.println("testing");
////
//        }
//        facereadersocket.closeInstance();
//        long t = System.currentTimeMillis();
//        long end = t + 5000;
//        vicarvision.Vicarvision.start_alanyzing();
//        while (System.currentTimeMillis() < end) {
//            float vals = vicarvision.Vicarvision.getValance();
//            System.out.println(vals + " = valance");
////            System.out.println("testing");
//
//        }
//        facereadersocket.closeInstance();
    }

    public static void start_alanyzing() throws UnsupportedEncodingException, IOException {

//        String start_detaillog_xml = "D:\\Users\\Tom\\Documents\\NetBeansProjects\\cfr\\src\\main\\java\\vicarvision\\xml\\start_detaillog.xml";
//        String start_detaillog_xml = "C:\\Documents and Settings\\Amfi\\My Documents\\NetBeansProjects\\cfr\\src\\main\\java\\vicarvision\\xml\\start_detaillog.xml";
        String start_detaillog_xml = "C:\\Users\\User\\Documents\\NetBeansProjects\\cfr\\src\\main\\java\\vicarvision\\xml\\start_detaillog.xml";
        String type = "FaceReaderAPI.Messages.ActionMessage";
        byte[] start_detaillog = apiencrypt.encrypt(start_detaillog_xml, type);

//        String start_analysing_xml = "D:\\Users\\Tom\\Documents\\NetBeansProjects\\cfr\\src\\main\\java\\vicarvision\\xml\\start_analysation.xml";
//        String start_analysing_xml = "C:\\Documents and Settings\\Amfi\\My Documents\\NetBeansProjects\\cfr\\src\\main\\java\\vicarvision\\xml\\start_analysation.xml";
        String start_analysing_xml = "C:\\Users\\User\\Documents\\NetBeansProjects\\cfr\\src\\main\\java\\vicarvision\\xml\\start_analysation.xml";
        byte[] start_analysing = apiencrypt.encrypt(start_analysing_xml, type);
        /* write some data */

        OutputStream socketOutputStream = facereadersocket.getInstance().getOutputStream();
        socketOutputStream.write(start_detaillog);
        socketOutputStream.write(start_analysing);
    }

    public static void getPerson() {
        Person person = new Person();

        try {
            InputStream stream = facereadersocket.getInstance().getInputStream();
            int dataInt = 0;
            while ((dataInt = stream.read()) != -1) {

                byte[] data = new byte[16000000];
                int count = stream.read(data);
                data = Arrays.copyOfRange(data, 0, count);

                if (data[0] == 11) {
                    data = Arrays.copyOfRange(data, 40, count);
                    String output = new String(data);
                    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
                    DocumentBuilder b = f.newDocumentBuilder();
                    Document doc = b.parse(new ByteArrayInputStream(output.getBytes("UTF-8")));
                    NodeList classification_Value = doc.getElementsByTagName("ClassificationValue");
                    for (int i = 0; i < classification_Value.getLength(); i++) {
                        Element label = (Element) classification_Value.item(i);
                        Node title_label = label.getElementsByTagName("Label").item(0);
                        //System.out.println(title_label.getTextContent());

                        Element value = (Element) classification_Value.item(i);
                        Node title_value = value.getElementsByTagName("string").item(0);
                        if ("Gender".equals(title_label.getTextContent())) {
                            if (title_value != null) {
                                person.setPerson_gender(title_value.getTextContent());
                            }
                        }
                        if ("Age".equals(title_label.getTextContent())) {
                            if (title_value != null) {
                                person.setPerson_age(title_value.getTextContent());
                            }
                        }

//                        if (person.getPerson_age() != null) {
//                            if (person.getPerson_gender() != null) {
//                                return person;
//                            }
//                        }


                    }
                    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
                    Date date = new Date();
                    person.setPerson_date(dateFormat.format(date));
//                    
                    PersonDAO.insertPerson(person);
                    return;
                }


//                    System.out.println(count);
//                    String out = new String(data);
//                    System.out.println(out);
//                    System.out.println("--------------------------------------");
//                    System.out.println(data[0]);
            }
//                server.close();
        } catch (Exception e) {
            System.out.println(e);
        }

        return;



    }

    public static float getValance() {
        float valu = 0;
        try {
            /* create address */
//            InetAddress serveraddress = InetAddress.getByName("127.0.0.1");

            try {
//                Socket server = new Socket(serveraddress, 9090);




                InputStream stream = facereadersocket.getInstance().getInputStream();
                int dataInt = 0;
                while ((dataInt = stream.read()) != -1) {

                    byte[] data = new byte[16000000];
                    int count = stream.read(data);
                    data = Arrays.copyOfRange(data, 0, count);
//                    System.out.print(data[0]);

//                    System.out.print((char)data[8]);
//                    System.out.println((char)data[9]);
//                    System.out.println((char)data[10]);

                    if (data[0] == 11) {
//                        System.out.println("data");
                        data = Arrays.copyOfRange(data, 40, count);
                        String output = new String(data);
                        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
                        DocumentBuilder b = f.newDocumentBuilder();
                        Document doc = b.parse(new ByteArrayInputStream(output.getBytes("UTF-8")));
                        NodeList classification_Value = doc.getElementsByTagName("ClassificationValue");
                        for (int i = 0; i < classification_Value.getLength(); i++) {
                            Element label = (Element) classification_Value.item(i);
                            Node title_label = label.getElementsByTagName("Label").item(0);
                            //System.out.println(title_label.getTextContent());

                            Element value = (Element) classification_Value.item(i);
                            Node title_value = value.getElementsByTagName("float").item(0);
                            if ("Valence".equals(title_label.getTextContent())) {
                                if (title_value != null) {
//
                                    valu = Float.parseFloat(title_value.getTextContent());
                                    if (valu > 1) {
                                        return 0;
                                    } else {
                                        return valu;
                                    }
//                                    System.out.println(valu);
//                                    System.out.println(valu); 

                                }
                            }

                        }
                    }


//                    System.out.println(count);
//                    String out = new String(data);
//                    System.out.println(out);
//                    System.out.println("--------------------------------------");
//                    System.out.println(data[0]);
                }
//                server.close();
            } catch (Exception e) {
                System.out.println(e);
            }

        } catch (Exception e) {
            System.out.println(e);
        }
        return valu;



    }
}
